import org.apache.zookeeper.ZooKeeper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;

/**
 * @ClassName ZKConnectSessionWatcher
 * @Description TODO
 * @Author BossZ
 * @Version 1.0
 * @Date 2019/1/15-18:19
 **/
public class ZKConnectSessionWatcher {
    public static final String zkServerPath = "192.168.137.12:2181";
    public static final Integer zkTimeOut = 200;
    public static final Logger log = LoggerFactory.getLogger(ZKConnectSessionWatcher.class);

    public static void main(String[] args) throws Exception {
        ZooKeeper zk = new ZooKeeper(zkServerPath, zkTimeOut, (e) -> {
            log.info("-----" + e.getState().name());
        });

        log.info("客户端开始连接zookeeper");
        log.info("连接状态:{}" + zk.getState());
        long sessionId = zk.getSessionId();
        byte[] sessionPasswd = zk.getSessionPasswd();
        TimeUnit.SECONDS.sleep(3);


        log.info("会话重连");
        ZooKeeper zooKeeper = new ZooKeeper(zkServerPath, zkTimeOut, (e) -> log.info("", e), sessionId, sessionPasswd);
        TimeUnit.SECONDS.sleep(10);
        log.info("连接状态" + zooKeeper.getState());
    }
}
